<template>
  <div>
    <el-container>
      <el-main style="margin-left:-12px;font-weight: bolder;">{{id? '编辑装备':'新建装备'}}</el-main>
    </el-container>
    <!-- native指向原生表单  -->
    <el-form label-width="80px" @submit.native.prevent="save">
      <el-form-item label="装备名称">
        <el-input style="width: 280px;" prefix-icon="el-icon-user" error required clearable maxlength = "20" v-model="model.name" placeholder="name"></el-input>
      </el-form-item>

      <el-form-item label="装备图标">
        <el-upload
          class="avatar-uploader"
          required
          :action="$http.defaults.baseURL+'/upload'"
          :show-file-list="false"
          :before-upload="beforeAvatarUpload"
          :on-success="afterUpload">
          <img v-if="model.icon" :src="model.icon" class="avatar" />

          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" native-type="submit" :plain="true">保存</el-button>
        <el-button type="danger" size="medium" native-type="button" @click="cancel" v-show="this.id" :plain="true">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {}
    };
  },
  created() {
    // this.fetchParents();
    this.id && this.fetch();
  },
  methods: {
    async save() {
      //实现新增的保存和编辑的保存
      if (this.id) {
        const response = await this.$http.put(
          `rest/items/${this.id}`,
          this.model
        );
      } else {
        const response = await this.$http.post("rest/items", this.model);
      }

      this.$router.push("/items/list");
      this.$message({
        type: "success",
        message: "保存成功",
        duration: 2000
      });
    },
    async fetch() {
      const response = await this.$http.get(`rest/items/${this.id}`);
      this.model = response.data;
    },
    //上传文件自动赋值名称
    beforeAvatarUpload(file){
      let isLt2M = file.size / 1024 / 1024 < 2;

      let imageName = file.name;
      this.model.name = imageName.split('.')[0]
      console.log(file);

        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isLt2M;
    },
    //图片保存成功返回操作
    afterUpload(response) {

      this.$message({
        type: "success",
        message: "上传成功",
        duration: 2000
      });

      this.$set(this.model, 'icon', response.url)
    },
    //取消保存返回上一页
    cancel() {
      this.$router.go(-1);
    }
  }
};
</script>

<style scope>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.el-icon-plus:before {
  top: 50%;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-button--medium {
  padding: 15px 60px !important;
}
</style>
